<?php

namespace System\Core;

/**
 * Description of Language
 *
 * @author elton.schivei
 */
class Language extends \System\Types\Object
{

    /**
     * @var \System\Core\ILanguage
     */
    public $language;
    public $is_loaded = array();

    public function __construct()
    {
        \System\Lib\Log::Write("Language Class Initialized", \System\Lib\Log::DEBUG);
        $this->Load(null);
    }

    // --------------------------------------------------------------------

    /**
     * Load a language file
     *
     * @access	public
     * @param	mixed	the name of the language file to be loaded. Can be an array
     * @param	string	the language (english, etc.)
     * @return	\System\Core\ILanguage
     */
    public function &Load($idiom = '\System\Language\English')
    {
        if (!$idiom)
        {
            $language = \config_item('language');
            $idiom = $language ? : '\System\Language\English';
        }

        $lang = @$this->is_loaded[$idiom]? : new $idiom();

        $this->is_loaded[$idiom] = $lang;

        $this->language = $this->is_loaded[$idiom];

        \System\Lib\Log::Write('Language file loaded: ' . $idiom, \System\Lib\Log::DEBUG);

        return $this->language;
    }

    // --------------------------------------------------------------------

    /**
     * Fetch a single line of text from the language array
     *
     * @access  public
     * @param   \System\Types\Object    $line   the language line
     * @return  \System\Types\String
     */
    public function Line(\System\Types\Object $line)
    {
        $lineUp = $this->language->Line($line);

        // Because killer robots like unicorns!
        if (!$lineUp)
        {
            \System\Lib\Log::Write('Could not find the language line "' . $line->ToString() . '"');
        }

        return $lineUp;
    }

}
